package shc.graduation.http.task;

import android.os.Handler;
import android.os.Looper;

import java.util.ArrayList;
import java.util.List;

import shc.graduation.http.getter.BaseGetter;
import shc.graduation.http.listener.TaskListener;

/**
 * Created by shuhuiChen on  2016/5/21  14:11.
 * Email ：shuhuiCSH@126.com
 */
public abstract class BaseTask<T> implements ITask {


    private TaskListener mTaskListener = null;
    public TaskOption mTaskOption = null;
    public BaseGetter mBaseGetter = null;

    public List<T> mModelList = new ArrayList<>();
    private Handler mHandler = new Handler(Looper.getMainLooper());
    private BaseTask mBaseTask = null;

    @Override
    public void executeTask(TaskOption taskOption, TaskListener taskListener) {
        this.mTaskListener = taskListener;
        this.mTaskOption = taskOption;
        this.mBaseTask = this;
        executeTask();
    }

    public void setGetter(BaseGetter iBaseGetter) {
        this.mBaseGetter = iBaseGetter;
    }

    abstract void executeTask();

    public void ListenerRunning(final String runningInfo) {
        if (mTaskListener != null) {
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    mTaskListener.running(mBaseTask, runningInfo);
                }
            });
        }
    }

    public void ListenerSuccess(final String successInfo) {
        if (mTaskListener != null || mBaseTask != null) {
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    mTaskListener.success(mBaseTask, successInfo);
                }
            });
        }
    }

    public void ListenerFail(final String failInfo) {
        if (mTaskListener != null) {
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    mTaskListener.fail(mBaseTask, failInfo);
                }
            });
        }
    }

}
